package com.jade.linkong.controller;

import com.jade.common.annotation.Log;
import com.jade.common.constant.UserConstants;
import com.jade.common.core.controller.BaseController;
import com.jade.common.core.domain.AjaxResult;
import com.jade.common.core.domain.Ztree;
import com.jade.common.enums.BusinessType;
import com.jade.common.utils.poi.ExcelUtil;
import com.jade.framework.util.ShiroUtils;
import com.jade.linkong.domain.LkMatterType;
import com.jade.linkong.service.ILkMatterTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 事项类型Controller
 * 
 * @author jade
 * @date 2020-07-16
 */
@Controller
@RequestMapping("/linkong/matterType")
public class LkMatterTypeController extends BaseController
{
    private String prefix = "linkong/matterType";

    @Autowired
    private ILkMatterTypeService lkMatterTypeService;

    @RequiresPermissions("linkong:matterType:view")
    @GetMapping()
    public String type()
    {
        return prefix + "/matterType";
    }

    /**
     * 查询事项类型列表
     */
    @RequiresPermissions("linkong:matterType:list")
    @PostMapping("/list")
    @ResponseBody
    public List<LkMatterType> list(LkMatterType lkMatterType)
    {
//        startPage();
        List<LkMatterType> list = lkMatterTypeService.selectLkMatterTypeList(lkMatterType);
        return list;
//        return getDataTable(list);
    }

    /**
     * 导出事项类型列表
     */
    @RequiresPermissions("linkong:matterType:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LkMatterType lkMatterType)
    {
        List<LkMatterType> list = lkMatterTypeService.selectLkMatterTypeList(lkMatterType);
        ExcelUtil<LkMatterType> util = new ExcelUtil<LkMatterType>(LkMatterType.class);
        return util.exportExcel(list, "type");
    }

    /**
     * 新增
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
    	LkMatterType menu = null;
        if (0L != parentId)
        {
            menu = lkMatterTypeService.selectLkMatterTypeById(parentId);
        }
        else
        {
            menu = new LkMatterType();
            menu.setTypeId(0L);
            menu.setTypeName("主目录");
        }
        mmap.put("type", menu);
        return prefix + "/add";
    }

    /**
     * 新增保存事项类型
     */
    @RequiresPermissions("linkong:matterType:add")
    @Log(title = "事项类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated LkMatterType lkMatterType)
    {
    	if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(lkMatterTypeService.checkLkMatterTypNameUnique(lkMatterType)))
        {
            return error("新增分类'" + lkMatterType.getTypeName() + "'失败，分类名称已存在");
        }
    	lkMatterType.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(lkMatterTypeService.insertLkMatterType(lkMatterType));
    }

    /**
     * 修改事项类型
     */
    @GetMapping("/edit/{typeId}")
    public String edit(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
    	mmap.put("lkMatterType", lkMatterTypeService.selectLkMatterTypeById(typeId));
        return prefix + "/edit";
    }

    /**
     * 修改保存事项类型
     */
    @RequiresPermissions("linkong:matterType:edit")
    @Log(title = "事项类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LkMatterType lkMatterType)
    {
    	if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(lkMatterTypeService.checkLkMatterTypNameUnique(lkMatterType)))
        {
            return error("修改分类'" + lkMatterType.getTypeName() + "'失败，分类名称已存在");
        }
        else if (lkMatterType.getParentId().equals(lkMatterType.getTypeId()))
        {
            return error("修改分类'" + lkMatterType.getTypeName() + "'失败，上级部门不能是自己");
        }
    	lkMatterType.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(lkMatterTypeService.updateLkMatterType(lkMatterType));
    }

    /**
     * 删除事项类型
     */
    @RequiresPermissions("linkong:matterType:remove")
    @Log(title = "事项类型", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{typeId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("typeId") Long typeId)
    {
    	 if (lkMatterTypeService.selectTypeCount(typeId) > 0)
         {
             return AjaxResult.warn("存在下级部门,不允许删除");
         }
    	 ShiroUtils.clearCachedAuthorizationInfo();
        return toAjax(lkMatterTypeService.deleteLkMatterTypeById(typeId));
    }
    
    /**
     * 选择分类树
     */
    @GetMapping("/selectTypeTree/{typeId}")
    public String selectTypeTree(@PathVariable("typeId") Long typeId, ModelMap mmap)
    {
        mmap.put("type", lkMatterTypeService.selectLkMatterTypeById(typeId));
        return prefix + "/tree";
    }

    /**
     * 加载分类列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = lkMatterTypeService.treeData();
        return ztrees;
    }
    
}
